package cn.iocoder.yudao.module.shop.controller.admin.itemtimesrecharge;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.itemtimesrecharge.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.itemtimesrecharge.ShopItemTimesRechargeDO;
import cn.iocoder.yudao.module.shop.convert.itemtimesrecharge.ShopItemTimesRechargeConvert;
import cn.iocoder.yudao.module.shop.service.itemtimesrecharge.ShopItemTimesRechargeService;

@Tag(name = "管理后台 - 项目次数充值")
@RestController
@RequestMapping("/shop/item-times-recharge")
@Validated
public class ShopItemTimesRechargeController {

    @Resource
    private ShopItemTimesRechargeService itemTimesRechargeService;

    @PostMapping("/create")
    @Operation(summary = "创建项目次数充值")
    @PreAuthorize("@ss.hasPermission('shop:item-times-recharge:create')")
    public CommonResult<Long> createItemTimesRecharge(@Valid @RequestBody ShopItemTimesRechargeCreateReqVO createReqVO) {
        return success(itemTimesRechargeService.createItemTimesRecharge(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目次数充值")
    @PreAuthorize("@ss.hasPermission('shop:item-times-recharge:update')")
    public CommonResult<Boolean> updateItemTimesRecharge(@Valid @RequestBody ShopItemTimesRechargeUpdateReqVO updateReqVO) {
        itemTimesRechargeService.updateItemTimesRecharge(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目次数充值")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:item-times-recharge:delete')")
    public CommonResult<Boolean> deleteItemTimesRecharge(@RequestParam("id") Long id) {
        itemTimesRechargeService.deleteItemTimesRecharge(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目次数充值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:item-times-recharge:query')")
    public CommonResult<ShopItemTimesRechargeRespVO> getItemTimesRecharge(@RequestParam("id") Long id) {
        ShopItemTimesRechargeDO itemTimesRecharge = itemTimesRechargeService.getItemTimesRecharge(id);
        return success(ShopItemTimesRechargeConvert.INSTANCE.convert(itemTimesRecharge));
    }

    @GetMapping("/list")
    @Operation(summary = "获得项目次数充值列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:item-times-recharge:query')")
    public CommonResult<List<ShopItemTimesRechargeRespVO>> getItemTimesRechargeList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopItemTimesRechargeDO> list = itemTimesRechargeService.getItemTimesRechargeList(ids);
        return success(ShopItemTimesRechargeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目次数充值分页")
    @PreAuthorize("@ss.hasPermission('shop:item-times-recharge:query')")
    public CommonResult<PageResult<ShopItemTimesRechargeRespVO>> getItemTimesRechargePage(@Valid ShopItemTimesRechargePageReqVO pageVO) {
        PageResult<ShopItemTimesRechargeDO> pageResult = itemTimesRechargeService.getItemTimesRechargePage(pageVO);
        return success(ShopItemTimesRechargeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目次数充值 Excel")
    @PreAuthorize("@ss.hasPermission('shop:item-times-recharge:export')")
    @OperateLog(type = EXPORT)
    public void exportItemTimesRechargeExcel(@Valid ShopItemTimesRechargeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopItemTimesRechargeDO> list = itemTimesRechargeService.getItemTimesRechargeList(exportReqVO);
        // 导出 Excel
        List<ShopItemTimesRechargeExcelVO> datas = ShopItemTimesRechargeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "项目次数充值.xls", "数据", ShopItemTimesRechargeExcelVO.class, datas);
    }

}
